package itbour.onetouchshow.listener;

import android.content.Intent;

import itbour.onetouchshow.activity.login.LoginActivity;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.SpUtils;
import itbour.onetouchshow.utils.UIUtils;


/**
 * 创建人：$ gyymz1993
 */

public class LogingStatusListener {
    private static LogingStatusListener listener = new LogingStatusListener();

    private LogingStatusListener() {
    }

    public static LogingStatusListener getInstance() {
        return listener;
    }

    public void checkLogin(LoginForCallBack callBack) {
        //L_.e("是否登录："+SpUtils.getInstance().getBoolean("isLogin",false));
        if (SpUtils.getInstance().getBoolean(AppConst.ISLOGIN, false)) { // 判断是否登录，否返回true
            callBack.callBack();
        } else {
            Configure.CALLBACK = new Configure.ICallBack() {
                @Override
                public void postExec() {
                    if (SpUtils.getInstance().getBoolean(AppConst.ISLOGIN, false)) {
                        callBack.callBack();
                        Configure.CALLBACK = null;
                    }
                }
            };

            Intent intent = new Intent(UIUtils.getContext(), LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            UIUtils.getContext().startActivity(intent);
        }
    }

    @FunctionalInterface//Java8 函数注解，没有升级java8的去掉这一句
    public interface LoginForCallBack {
        void callBack();
    }

    public static class Configure {
        // 登录回调接口
        public static ICallBack CALLBACK;

        // 声明一个登录成功回调的接口
        public interface ICallBack {
            // 在登录操作及信息获取完成后调用这个方法来执行登录回调需要做的操作
            void postExec();
        }

    }
}
